package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/08 21:37
 **/
public class Day6_12_Num200_NumIslands {
    class Solution {
        public int numIslands(char[][] grid) {
            int ans = 0, m = grid.length, n = grid[0].length;
            for(int i = 0; i < m; ++i){
                for(int j = 0; j < n; ++j){
                    if(grid[i][j] == '1'){
                        dfs(grid, i, j, m, n);
                        ans++;
                    }
                }
            }
            return ans;
        }
        private void dfs(char[][] grid, int i, int j, int m, int n){
            if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == '0' || grid[i][j] == '2')
                return;
            grid[i][j] = '2';
            dfs(grid, i-1, j, m, n);
            dfs(grid, i+1, j, m, n);
            dfs(grid, i, j-1, m, n);
            dfs(grid, i, j+1, m, n);
        }
    }
}